/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.action;

import com.myapp.form.GrosClientForm;
import com.myapp.form.SimulationOrdreForm;
import com.myapp.metier.ChargeServer;
import com.myapp.metier.Helper;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author user
 */
public class GrosClientAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        GrosClientForm formBean = (GrosClientForm) form;

        Integer nbClient = formBean.getNumber();
        long start = System.currentTimeMillis();
        
        ChargeServer.charge += nbClient;
        for (int i = 0; i < nbClient; i++) {
            Helper helper = new Helper();

            int typeOperation = 1 + (int) (Math.random() * 2);
            int compteClient = 1132 + (int) (Math.random() * 76327);
            int quantity = 1 + (int) (Math.random() * 1000);
            int price = 1000 + (int) (Math.random() * 10000);
            String typeClient = "Gros clients";

            helper.addOrder(typeOperation, compteClient, quantity, price, typeClient);
        }
        ChargeServer.charge -= nbClient;
        
        long end = System.currentTimeMillis();
        long executionTime = end - start;
        request.setAttribute("executionTime", executionTime);
        return mapping.findForward(SUCCESS);
    }
}
